const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../services/user.js');
const app = getApp();
var isSubmit = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payments: [
      // {"val":"wechat","name":"微信"},
      // { "val": "alipay", "name": "支付宝" }
    ],
    paymentsIndex: 0,
    commission: 0,
    showBankName: true,
    mobile: "",
    name: "",
    bankName: "",
    amount: "",
    account: ""
  },

  //选择收款方式 
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      paymentsIndex: e.detail.value
    });
    if (this.data.payments[e.detail.value / 1].val == "bank") {
      this.setData({
        showBankName: true
      });
    } else {
      this.setData({
        showBankName: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    isSubmit = false;
    var that = this;
    util.request(api.MemberInfoUrl, {}, 'GET').then(function (data) {
      if (data.type === 'success') {
        that.setData({
          memberId: data.data.member.id,
          member: data.data.member,
          commission: data.data.member.commission,
          historyCommission: data.data.member.historyCommission,
          withdrawalMin: data.data.withdrawalMin,
          payments: data.data.payments,
          name: data.data.member.name ? data.data.member.name : "",
          mobile: data.data.member.mobile ? data.data.member.mobile : ""
        });
      } else {
        console.log(data.content);
      }
    });
  },


  nameInput: function (event) {
    this.setData({
      name: event.detail.value
    });
  },
  mobileInput: function (event) {
    this.setData({
      mobile: event.detail.value
    });
  },
  bankNameInput: function (event) {
    this.setData({
      bankName: event.detail.value
    });
  },
  accountInput: function (event) {
    this.setData({
      account: event.detail.value
    });
  },
  amountInput: function (event) {
    this.setData({
      amount: event.detail.value
    });
  },
  amountBlur: function (event) {
    var amount = event.detail.value / 1;
    this.setData({
      amount: amount.toFixed(2)
    });
  },
  apply: function () {
    if (isSubmit) {
      return;
    }

    var name = this.data.name;
    var mobile = this.data.mobile;
    var bankName = this.data.bankName;
    var account = this.data.account;
    var amount = this.data.amount;
    var paymentsIndex = this.data.paymentsIndex;
    var payment = this.data.payments[paymentsIndex].val;

    if (!name) {
      util.showSuccessToast("请填写姓名");
      return;
    }
    if (!mobile) {
      util.showSuccessToast("请填写手机号");
      return;
    }
    if (payment == "bank") {
      if (!bankName) {
        util.showSuccessToast("请填写开户行");
        return;
      }
    }
    if (!account) {
      util.showSuccessToast("请填写提现账户");
      return;
    }
    if (!amount) {
      util.showSuccessToast("请填写提现金额");
      return;
    }
    if (amount < this.data.withdrawalMin) {
      util.showSuccessToast("最小提现金额：" + this.data.withdrawalMin);
      return;
    }
    if (amount > this.data.commission) {
      util.showSuccessToast("提现金额不能大于当前可提现金额：￥" + this.data.commission);
      return;
    }
    wx.showLoading({
      title: '提交中...',
    })
    isSubmit = true;
    var data = {
      name: this.data.name,
      mobile: this.data.mobile,
      bankName: this.data.bankName,
      account: this.data.account,
      amount: this.data.amount,
      payment: payment
    }
    util.request(api.ApplyWithdrawalUrl, data, 'POST').then(function (data) {
      wx.hideLoading();
      if (data.type === 'success') {
        util.showSuccessToast(data.content);
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      } else {
        isSubmit = false;
        util.showErrorToast(data.content);
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})